package vn.vfriends.vcms.admin.bean;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.vcms.admin.model.Category;
import vn.vfriends.vcms.admin.model.Item;
import vn.vfriends.vcms.admin.service.CategoryService;
import vn.vfriends.vcms.admin.service.ItemService;

/**
 *
 * @author tuan@vfriends.vn
 */
@Named
@ConversationScoped
public class OrderListBean implements Serializable {
    
    private Logger logger = LoggerFactory.getLogger(OrderListBean.class);
    
    @Inject private Conversation conversation;
    
    @Inject private CategoryService categoryService;
    
    @Inject private ItemService itemService;
    
    private Category category;
    
    private List<Category> parentCategories;
    
    private List<Category> orderedCategories;
    
    private List<Item> orderedItems;
    
    @PostConstruct
    public void init(){
        if (conversation.isTransient()) {
            conversation.begin();
        }
        
        parentCategories = categoryService.findParentCategories();
        orderedCategories = categoryService.findOrderedCategories(category);
        orderedItems = itemService.findOrderedItems(category);
    }
    
    public Conversation getConversation() {
        return conversation;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Category getCategory() {
        return category;
    }

    public List<Category> getParentCategories() {
        return parentCategories;
    }

    public List<Category> getOrderedCategories() {
        return orderedCategories;
    }

    public void setOrderedCategories(List<Category> orderedCategories) {
        this.orderedCategories = orderedCategories;
    }

    public List<Item> getOrderedItems() {
        return orderedItems;
    }

    public void setOrderedItems(List<Item> orderedItems) {
        this.orderedItems = orderedItems;
    }
    
    public void handleCategoryChangeAction() {
        orderedCategories = categoryService.findOrderedCategories(category);
        orderedItems = itemService.findOrderedItems(category);
    }
    
    public void saveAction() {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            for(int i = 0; i < this.orderedCategories.size(); i++) {
                Category orderedCategory = this.orderedCategories.get(i);
                if(orderedCategory.getOrderNumber() != (i + 1)) {
                    orderedCategory.setOrderNumber(i+1);
                    this.categoryService.editCategory(orderedCategory);
                }
            }
            
            for(int i = 0; i < this.orderedItems.size(); i++) {
                Item orderedItem = this.orderedItems.get(i);
                if(orderedItem.getOrderNumber() != (i + 1)) {
                    orderedItem.setOrderNumber(i+1);
                    this.itemService.editItem(orderedItem);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }
    }
}
